﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using Newtonsoft.Json;
using System.IO;

public class AIDocumentEditorWindow : EditorWindow
{
	protected List<TransitionLevel> EditorLevels { get; set; }
	protected string SavePath { get; set; }
	protected TextAsset CurTextAsset { get; set; }
	protected EditorWindow CurWindow { get; set; }
	protected int editIndex = 0;
	protected int EditIndex
	{
		get {
			if (editIndex >= EditorLevels.Count)
				return editIndex = EditorLevels.Count - 1;
			else if (editIndex < 0)
				return 0;
			else
				return editIndex;

		}
		set => editIndex = value;
	}


	[MenuItem("Assets/Editor/AIJsonFile")]
	static void OpenWindow()
	{
		AIDocumentEditorWindow window = GetWindow<AIDocumentEditorWindow>();
		if (window.InitParam(window))
		{
			window.Show();
		}
	}

	private bool InitParam(EditorWindow window)
	{
		EditorLevels = new List<TransitionLevel>();
		CurWindow = window;
		TextAsset text = Selection.activeObject as TextAsset;
		SavePath = AssetDatabase.GetAssetPath(text);
		if (string.IsNullOrWhiteSpace(text.text))
			return false;
		try
		{
			List<TransitionLevel> levels = JsonConvert.DeserializeObject<List<TransitionLevel>>(text.text);
			if (null != levels && levels.Count > 0)
			{
				EditorLevels = levels;
				CurTextAsset = text;
				return true;
			}else
				return false;

		}
		catch(Exception e)
		{
			this.Close();
			Debug.Log(e.StackTrace);
		}
		return false;
	}


	//GUIStyle styleArea = new GUIStyle(GUI.skin.box);
	private void OnGUI()
	{
		//MainStart
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField($"当前编辑文件 {SavePath}",new GUIStyle("AssetLabel") { alignment = TextAnchor.MiddleCenter});
		//EditorGUI.BeginDisabledGroup(true);
		//EditorGUILayout.ObjectField("当前编辑文件 :", CurTextAsset, typeof(TextAsset), false);
		//EditorGUI.EndDisabledGroup();
		float top = EditorGUIUtility.singleLineHeight + 10;

		//Area1
		var styleArea = new GUIStyle(GUI.skin.box);
		GUILayout.BeginArea(new Rect(0, top, 0.2f * CurWindow.position.width, CurWindow.position.height - 20), styleArea);
		var styleMiddleLable = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter };
		EditorGUILayout.LabelField("Levels", styleMiddleLable);
		EditorGUILayout.BeginHorizontal();
		if (GUILayout.Button(new GUIContent("Add")))
		{
			AddLevel();
		}
		GUILayout.Space(5);
		if (GUILayout.Button("Insert"))
		{
			InsertLevel();
		}
		GUILayout.Space(5);
		if (GUILayout.Button("Delete"))
		{
			DeleteLevel();
		}
		EditorGUILayout.EndHorizontal();
		GUILayout.Space(5);
		for (int index = 0; index < EditorLevels.Count; index++)
		{
			if(GUILayout.Button($"Level {index} ", index == EditIndex ? new GUIStyle("Button") { fontStyle = FontStyle.Bold } : new GUIStyle("Button")))
			{
				EditIndex = index;
			}
		}

		GUILayout.EndArea();



		//Area2

		//var styleArea = new GUIStyle(GUI.skin.box);
		GUILayout.BeginArea(new Rect(0.2f * CurWindow.position.width + 0.01f * CurWindow.position.width, top, 0.8f * CurWindow.position.width - 0.01f * CurWindow.position.width, CurWindow.position.height - 20), styleArea);
		EditorGUILayout.LabelField("Level Infor", styleMiddleLable);

		EditorGUILayout.LabelField(TransitionParamType.Prtrol.ToString(), styleMiddleLable);
		EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].Priority 
			= EditorGUILayout.IntField("优先级:", EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].Priority);
		EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilancePatrol 
			= EditorGUILayout.FloatField("重定向范围 :", EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilancePatrol);
		EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilanceEscape =
			EditorGUILayout.FloatField("逃离转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilanceEscape);
		EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilanceChase =
			EditorGUILayout.FloatField("追踪转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].VigilanceChase);
		EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].ChaseMaxTime =
			EditorGUILayout.FloatField("追踪时间 :", EditorLevels[EditIndex].Params[TransitionParamType.Prtrol].ChaseMaxTime);

		EditorGUILayout.LabelField(TransitionParamType.Escape.ToString(), styleMiddleLable);
		EditorLevels[EditIndex].Params[TransitionParamType.Escape].Priority =
			EditorGUILayout.IntField("优先级:", EditorLevels[EditIndex].Params[TransitionParamType.Escape].Priority);
		EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilanceEscape =
			EditorGUILayout.FloatField("重定向范围 :", EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilanceEscape);
		EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilancePatrol =
			EditorGUILayout.FloatField("游荡转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilancePatrol);
		EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilanceChase =
			EditorGUILayout.FloatField("追踪转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Escape].VigilanceChase);
		EditorLevels[EditIndex].Params[TransitionParamType.Escape].ChaseMaxTime =
			EditorGUILayout.FloatField("追踪时间 :", EditorLevels[EditIndex].Params[TransitionParamType.Escape].ChaseMaxTime);

		EditorGUILayout.LabelField(TransitionParamType.Chase.ToString(), styleMiddleLable);
		EditorLevels[EditIndex].Params[TransitionParamType.Chase].Priority =
			EditorGUILayout.IntField("优先级:", EditorLevels[EditIndex].Params[TransitionParamType.Chase].Priority);
		EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilanceChase =
			EditorGUILayout.FloatField("重定向范围 :", EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilanceChase);
		EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilancePatrol =
			EditorGUILayout.FloatField("游荡转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilancePatrol);
		EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilanceEscape =
			EditorGUILayout.FloatField("逃离转态判定 :", EditorLevels[EditIndex].Params[TransitionParamType.Chase].VigilanceEscape);
		EditorLevels[EditIndex].Params[TransitionParamType.Chase].ChaseMaxTime =
			EditorGUILayout.FloatField("追踪时间 :", EditorLevels[EditIndex].Params[TransitionParamType.Chase].ChaseMaxTime);

		GUILayout.EndArea();
		//MainEnd
		EditorGUILayout.EndHorizontal();
	}

	protected void AddLevel()
	{
		EditorLevels.Add(new TransitionLevel());
	}

	protected void DeleteLevel()
	{
		EditorLevels.RemoveAt(EditIndex);
	}
	protected void InsertLevel()
	{
		EditorLevels.Insert(EditIndex + 1, new TransitionLevel());
	}

	protected void OnDestroy()
	{
		string newJson = JsonConvert.SerializeObject(EditorLevels);
		if (null != CurTextAsset && newJson != CurTextAsset.text)
		{
			if (EditorUtility.DisplayDialog("关闭窗口", "是否保存修改", "Svae", "Quit"))
			{
				Save(newJson);
			}
		}
	}

	protected void Save(string json)
	{
		using(StreamWriter sw = new StreamWriter(new FileStream(SavePath, FileMode.Create, FileAccess.Write)))
		{
			sw.Write(json);
			sw.Flush();
		}
		AssetDatabase.ImportAsset(SavePath);
	}
}
